package gui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.RenderingHints;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.Ellipse2D;

import javax.swing.JButton;

public class CustomJButton extends JButton{
	
	private static final long serialVersionUID = 1L;
	public static final Color RED = new Color(230,92,92);
	public static final Color BLUE = new Color(110, 192, 210);
	public static final Color GREEN = new Color(125, 198, 146);
	public static final Color YELLOW = new Color(223, 187, 65);
	public static final Color BROWN = new Color(216, 142, 100);
	public static final Color PUZZLE = new Color(221, 107,174);
	public static final Color LIGHTER = new Color(255,255,255,40);
	public static final Color DARKER = new Color(0,0,0,20);
	public static final Color LIGHTERBLUE = new Color(110, 192, 210,70);
	public static final Color LIGHTERRED = new Color(230,92,92,150);
	
	private Color bgColor;
	private Color movedOnColor;
	private int gap;
	private boolean isMovedOn;
	private boolean isClicked;
	private boolean isRound;
	private boolean isBorder;
	
	public CustomJButton(String title,int width, 
			int height, Color backgroundColor, Color movedOnColor, 
			Color stringColor, int gap, boolean isRound, boolean isBorder) {
		setText(title);
		setForeground(stringColor);
		setMargin(new Insets(0, 0, 0, 0));
		setPreferredSize(new Dimension(width, height));
		this.bgColor = backgroundColor;
		this.movedOnColor = movedOnColor;
		this.gap = gap;
		this.isRound = isRound;
		this.isBorder = isBorder;
		
		setFont(getFont().deriveFont(14f));
		setOpaque(true);
		setBorderPainted(false); 
		setContentAreaFilled(false); 
		setFocusPainted(false); 
		isMovedOn = false;
		isClicked =  false;
		addMouseListener(new CustomJButtonMouseListener());
	}
	
	public Color getBgColor() {
		return bgColor;
	}

	public void setBgColor(Color bgColor) {
		this.bgColor = bgColor;
	}

	public Color getMovedOnColor() {
		return movedOnColor;
	}

	public void setMovedOnColor(Color movedOnColor) {
		this.movedOnColor = movedOnColor;
	}

	public int getGap() {
		return gap;
	}

	public void setGap(int gap) {
		this.gap = gap;
	}

	@Override
	protected void paintComponent(Graphics g) {
		Graphics2D g2d = (Graphics2D) g;
		g2d.setColor(this.bgColor);
		if(!isRound){
			g2d.fillRect(0, 0, this.getWidth(), this.getHeight());//,true
			if(isBorder){
				g2d.setColor(Color.GRAY);
				g2d.drawRect(0, 0, this.getWidth()-1, this.getHeight()-1);
			}
		}else{
			g2d.setRenderingHint(
				    RenderingHints.KEY_ANTIALIASING,
				    RenderingHints.VALUE_ANTIALIAS_ON);
			Ellipse2D.Double circle = new Ellipse2D.Double(0, 0, this.getWidth()-1, this.getHeight()-1);
			g2d.fill(circle);
			if(isBorder){
				g2d.setColor(Color.GRAY);
				g2d.draw(circle);
			}
		}
		
		if(isMovedOn && isEnabled()){
			g2d.setColor(this.movedOnColor);
			if(!isRound){
				g2d.fillRect(gap, gap, this.getWidth()-1-gap*2, this.getHeight()-1-gap*2);
			}else{
				Ellipse2D.Double circle = new Ellipse2D.Double(gap, gap, this.getWidth()-gap*2, this.getHeight()-gap*2);
				g2d.fill(circle);
			}
		}
		if(isClicked && isEnabled()){
			g2d.setColor(new Color(255,255,255,80));
			if(!isRound){
				g2d.fillRect(gap, gap, this.getWidth()-1-gap*2, this.getHeight()-1-gap*2);
			}else{
				Ellipse2D.Double circle = new Ellipse2D.Double(gap, gap, this.getWidth()-gap*2, this.getHeight()-gap*2);
				g2d.fill(circle);
			}
			
		}
		g2d.setRenderingHint(
			    RenderingHints.KEY_ANTIALIASING,
			    RenderingHints.VALUE_ANTIALIAS_DEFAULT);
		super.paintComponent(g);
		
	}
	
	class CustomJButtonMouseListener extends MouseAdapter{

		@Override
		public void mouseExited(MouseEvent arg0) {
			super.mouseExited(arg0);
			if(isMovedOn){
				isMovedOn = false;
				repaint();
			}
		}

		@Override
		public void mouseEntered(MouseEvent arg0) {
			super.mouseEntered(arg0);
			if(!isMovedOn){
				isMovedOn = true;
				repaint();
			}
		}

		@Override
		public void mousePressed(MouseEvent arg0) {
			super.mousePressed(arg0);
			if(!isClicked){
				isClicked = true;
				repaint();
			}
		}

		@Override
		public void mouseReleased(MouseEvent arg0) {
			super.mouseReleased(arg0);
			if(isClicked){
				isClicked = false;
				repaint();
			}
		}
	}
}
